package com.linfnn.demo.Controller;
import com.linfnn.demo.Model.ExerciseProgram;
import com.linfnn.demo.Service.ExerciseProgramService;
import com.linfnn.demo.Service.impl.ExerciseProgramServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Timer;


@Controller
public class FunctionExerciseProgram {

    @Autowired
    private ExerciseProgramServiceImpl exerciseProgramService;

    //  这是管理页面，同时可以刷新数据，进行操作后跳到这里会重新获取数据，从而刷新页面
    @RequestMapping(value = "/manage_exercisePrograms")
    public ModelAndView showAll() {
        List<ExerciseProgram> exercisePrograms = exerciseProgramService.list();
        //        返回的页面
        ModelAndView mav = new ModelAndView("/managehtml/ExerciseProgramManage.html");
        //        页面和对应的数据结合
        mav.addObject("results", exercisePrograms);
        return mav;
    }

    //
    @PostMapping("/search/ExerciseProgram")
    public ModelAndView search(@RequestParam("ProjectName") String ProjectName) {
        ExerciseProgram exerciseProgram = exerciseProgramService.findExerciseProgramById(ProjectName);
        ModelAndView mav = new ModelAndView("/search/index3.html");
        mav.addObject("results", exerciseProgram);
        return mav;
    }

    //    注意这里返回应该更换为html，不能再去直接刷新获取整个数据库的数据， 并且这里的路由为searchadmin，前面有一个search，两者不一样
    @PostMapping("/searchadmin/ExerciseProgram")
    public ModelAndView searchadmin(@RequestParam("ProjectName") String ProjectName) {
        ExerciseProgram exerciseProgram = exerciseProgramService.findExerciseProgramById(ProjectName);
        ModelAndView mav = new ModelAndView("/managehtml/ExerciseProgramManage.html");
        mav.addObject("results", exerciseProgram);
        return mav;
    }


    @RequestMapping("/delete/ExerciseProgram")
    public ModelAndView deleteExerciseProgram(@RequestParam("Code") String Code) {
        // Delete assessment logic here
        if (Code.isEmpty()){
            System.out.println("delete输入为空");
        }
        if(exerciseProgramService.removeById(Code)){
            System.out.println("删除成功");
        };

        ModelAndView mav = new ModelAndView("redirect:/manage_exercisePrograms");
        return mav;
    }


    //    这里点击更新后，会跳转到xxxUpdata.html文件里面去，然后再执行更新
    @RequestMapping("/updatehtml/ExerciseProgram")
    public ModelAndView updateExerciseProgramhtml() {
        return new ModelAndView("redirect:/updatahtml/exerciseprogramUpdata.html");
    }

    @RequestMapping("/update/ExerciseProgram")
    public ModelAndView updateExerciseProgram(@RequestParam("Code") String Code,
                                         @RequestParam("ProjectName") String ProjectName,
                                         @RequestParam("ExerciseIntensity") String ExerciseIntensity,
                                         @RequestParam("StepEquivalent") String StepEquivalent,
                                         @RequestParam("ExerciseTime") String Exercisetime,
                                         @RequestParam("AgeRange") String AgeRange,
                                         @RequestParam("ExerciseMethod") String ExerciseMethod,
                                         @RequestParam("Precautions") String Precautions
    ) {
        // Update assessment logic here
//    这里逻辑比较复杂，为了方便，这里使用的是先删除对应数据，然后插入该数据的操作
        if(exerciseProgramService.removeById(Code)){
            System.out.println("更新——删除成功");
        };


        ExerciseProgram exerciseProgram = new ExerciseProgram();
        exerciseProgram.setCode(Code);
        exerciseProgram.setProjectName(ProjectName);
        exerciseProgram.setExerciseIntensity(ExerciseIntensity);
        exerciseProgram.setStepEquivalent(StepEquivalent);
        exerciseProgram.setExerciseTime(Exercisetime);
        exerciseProgram.setAgeRange(AgeRange);
        exerciseProgram.setExerciseMethod(ExerciseMethod);
        exerciseProgram.setPrecautions(Precautions);

        if(exerciseProgramService.save(exerciseProgram)){
            System.out.println("更新——添加成功");
        };

        return new ModelAndView("redirect:/manage_exercisePrograms");
    }



    @RequestMapping("/add/ExerciseProgram")
    public ModelAndView addAssessment(@RequestParam("Code") String Code,
                                      @RequestParam("ProjectName") String ProjectName,
                                      @RequestParam("ExerciseIntensity") String ExerciseIntensity,
                                      @RequestParam("StepEquivalent") String stepEquivalent,
                                      @RequestParam("ExerciseTime") String Exercisetime,
                                      @RequestParam("AgeRange") String AgeRange,
                                      @RequestParam("ExerciseMethod") String Exercisemethod,
                                      @RequestParam("Precautions") String Precautions){
        // Add assessment logic here
        ExerciseProgram exerciseProgram = new ExerciseProgram();
        exerciseProgram.setCode(Code);
        exerciseProgram.setProjectName(ProjectName);
        exerciseProgram.setExerciseIntensity(ExerciseIntensity);
        exerciseProgram.setStepEquivalent(stepEquivalent);
        exerciseProgram.setExerciseTime(Exercisetime);
        exerciseProgram.setAgeRange(AgeRange);
        exerciseProgram.setExerciseMethod(Exercisemethod);
        exerciseProgram.setPrecautions(Precautions);


        if(exerciseProgramService.save(exerciseProgram)){
            System.out.println("添加成功");
        };
        return new ModelAndView("redirect:/manage_exercisePrograms");
    }

}
